package com.ihr360.insurance.model.dto.ledger;

import com.ihr360.commons.model.type.LedgerItemDataSource;
import com.ihr360.insurance.entity.snapshot.BenefitSnapshot;
import com.ihr360.insurance.entity.snapshot.CompanyBenefitSnapshot;
import com.ihr360.insurance.model.meta.DynamicRow;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import com.ihr360.shared.entitywithlistener.NumberIdWithTimeAndCompanyId;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * 社保公积金月度台账统计汇总信息。
 * <pre>
 * <b>每个账号每月只有一份社保公积金台账</b>
 * <ul>
 *         <li>账单月份</li>
 *         <li>社保缴纳人数</li>
 *         <li>公积金缴纳人数</li>
 *         <li>社保缴费</li>
 *         <li>公积金缴费</li>
 *         <li>社保增员人数</li>
 *         <li>公积金增员人数</li>
 *         <li>社保停缴人数</li>
 *         <li>公积金停缴人数</li>
 * </table>
 * </pre>
 *
 * @author davidwei
 */
@Entity
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
@Table(name = "monthlyledger", indexes = {@Index(name = "company_staff_index", columnList = "companyId,staff_id"),
        @Index(name = "company_year_month_index", columnList = "companyId,ledgerYear,ledgerMonth")})
public class MonthlyLedger extends NumberIdWithTimeAndCompanyId<Long> implements Serializable {
    private static final long serialVersionUID = -9149737360741998656L;

    public static final class Field {
        public static final String id = "id";
        public static final String companyId = "companyId";
        public static final String userId = "userId";
        public static final String ledgerYear = "ledgerYear";
        public static final String ledgerMonth = "ledgerMonth";

        public static final String staff = "staff";
        public static final String staff_staffName = "staff.staffName";
        public static final String staff_idCardNo = "staff.idCardNo";
        public static final String staff_departmentId = "staff.departmentId";

        public static final String siCompanyBenefit_id = "siCompanyBenefit.id";
        public static final String hfCompanyBenefit_id = "hfCompanyBenefit.id";
    }

    /**
     * 用户id
     */
    @Column(length = 40)
    private String userId;

    /**
     * 台账所属年
     */
    @Column(nullable = false, length = 10)
    private Integer ledgerYear;

    /**
     * 台账所属月
     */
    @Column(nullable = false, length = 4)
    private Integer ledgerMonth;

    @ManyToOne
    @JoinColumn(nullable = false)
    private SyncedBasicStaffInfo staff;

    /**
     * 数据来源
     */
    @Column(nullable = false, length = 20)
    @Enumerated(EnumType.STRING)
    private LedgerItemDataSource dataSource;

    /**
     * 社保方案快照
     */
    @AttributeOverrides({
            @AttributeOverride(name = "id", column = @Column(name = "si_company_benefit_id")),
            @AttributeOverride(name = "name", column = @Column(name = "si_company_benefit_name"))
    })
    @Embedded
    private CompanyBenefitSnapshot siCompanyBenefit;

    /**
     * 公积金方案快照
     */
    @AttributeOverrides({
            @AttributeOverride(name = "id", column = @Column(name = "hf_company_benefit_id")),
            @AttributeOverride(name = "name", column = @Column(name = "hf_company_benefit_name"))
    })
    @Embedded
    private CompanyBenefitSnapshot hfCompanyBenefit;
    /**
     * 社保个人设置快照
     */
    @AttributeOverrides({
            @AttributeOverride(name = "base", column = @Column(name = "si_staff_benefit_base")),
            @AttributeOverride(name = "startOn", column = @Column(name = "si_staff_benefit_start_on")),
            @AttributeOverride(name = "endOn", column = @Column(name = "si_staff_benefit_end_on")),
            @AttributeOverride(name = "calculatable", column = @Column(name = "si_staff_benefit_calculatable"))
    })
    @Embedded
    private BenefitSnapshot staffSocialInsurance;

    /**
     * 公积金个人设置快照
     */
    @AttributeOverrides({
            @AttributeOverride(name = "base", column = @Column(name = "hf_staff_benefit_base")),
            @AttributeOverride(name = "startOn", column = @Column(name = "hf_staff_benefit_start_on")),
            @AttributeOverride(name = "endOn", column = @Column(name = "hf_staff_benefit_end_on")),
            @AttributeOverride(name = "calculatable", column = @Column(name = "hf_staff_benefit_calculatable"))
    })
    @Embedded
    private BenefitSnapshot staffHouseFund;

    @Column(columnDefinition = "text")
    @Type(type = "json")
    private DynamicRow cells;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getLedgerYear() {
        return ledgerYear;
    }

    public void setLedgerYear(Integer ledgerYear) {
        this.ledgerYear = ledgerYear;
    }

    public Integer getLedgerMonth() {
        return ledgerMonth;
    }

    public void setLedgerMonth(Integer ledgerMonth) {
        this.ledgerMonth = ledgerMonth;
    }

    public SyncedBasicStaffInfo getStaff() {
        return staff;
    }

    public void setStaff(SyncedBasicStaffInfo staff) {
        this.staff = staff;
    }

    public LedgerItemDataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(LedgerItemDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public CompanyBenefitSnapshot getSiCompanyBenefit() {
        return siCompanyBenefit;
    }

    public void setSiCompanyBenefit(CompanyBenefitSnapshot siCompanyBenefit) {
        this.siCompanyBenefit = siCompanyBenefit;
    }

    public CompanyBenefitSnapshot getHfCompanyBenefit() {
        return hfCompanyBenefit;
    }

    public void setHfCompanyBenefit(CompanyBenefitSnapshot hfCompanyBenefit) {
        this.hfCompanyBenefit = hfCompanyBenefit;
    }

    public BenefitSnapshot getStaffSocialInsurance() {
        return staffSocialInsurance;
    }

    public void setStaffSocialInsurance(BenefitSnapshot staffSocialInsurance) {
        this.staffSocialInsurance = staffSocialInsurance;
    }

    public BenefitSnapshot getStaffHouseFund() {
        return staffHouseFund;
    }

    public void setStaffHouseFund(BenefitSnapshot staffHouseFund) {
        this.staffHouseFund = staffHouseFund;
    }

    public DynamicRow getCells() {
        return cells;
    }

    public void setCells(DynamicRow cells) {
        this.cells = cells;
    }
}